from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
# Create your views here.
from backend.models import User
from backend.models import BaseData
from django.core import serializers
from django.core.serializers.json import DjangoJSONEncoder
import json



def hello(request):
    return HttpResponse("Hello World !")


def login(request):
    if request.method == 'GET':
        username = request.GET.get('username')
        password = request.GET.get('password')
    else:
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
    try:
        user = User.objects.get(user_name=username, password=password)
        return JsonResponse({"msg": "login sucess", "code": 1})
    except User.DoesNotExist as e:
        return JsonResponse({"msg": "login fail", "code": 0})


# 获取basedata
def getbasedata(request):
    if request.method == 'GET' or request.method == 'POST':
        try:
            outlist = []
            temp_output = serializers.serialize('python', BaseData.objects.all())
            for data in temp_output:
                outlist.append(data["fields"])
            outstr = json.dumps(outlist, cls=DjangoJSONEncoder)
        except Exception as e:
            pass
        return HttpResponse(outstr)
    else:
        return JsonResponse({"msg": "POST request only", "code": 400})


# 单条数据上传
def loaddatasingle(request):
    if request.method == 'GET' or request.method == 'POST':
        try:
            # 获取参数
            pass
        except Exception as e:
            pass
        pass
    else:
        return JsonResponse({"msg": "POST request only", "code": 400})


# 多条数据上传
def loaddatabatch(request):
    if request.method == 'GET' or request.method == 'POST':
        try:
            # 获取参数
            pass
        except Exception as e:
            pass
        pass
    else:
        return JsonResponse({"msg": "POST request only", "code": 400})


# 数据修改
def updateRecord(request):
    if request.method == 'GET' or request.method == 'POST':
        try:
            # 获取参数
            pass
        except Exception as e:
            pass
        pass
    else:
        return JsonResponse({"msg": "POST request only", "code": 400})
